package cn.iocoder.yudao.module.wms.controller.admin.rdrecord10;

import cn.iocoder.yudao.module.wms.controller.admin.rdrecord01.vo.Rdrecord01RespVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord01.Rdrecord01DO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.rdrecord10.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord10.Rdrecord10DO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord10.Rdrecords10DO;
import cn.iocoder.yudao.module.wms.service.rdrecord10.Rdrecord10Service;

@Tag(name = "管理后台 - 生产入库主")
@RestController
@RequestMapping("/wms/rdrecord10")
@Validated
public class Rdrecord10Controller {

    @Resource
    private Rdrecord10Service rdrecord10Service;

    @PostMapping("/create")
    @Operation(summary = "创建生产入库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:create')")
    public CommonResult<Integer> createRdrecord10(@Valid @RequestBody Rdrecord10SaveReqVO createReqVO) {
        return success(rdrecord10Service.createRdrecord10(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产入库主")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:update')")
    public CommonResult<Boolean> updateRdrecord10(@Valid @RequestBody Rdrecord10SaveReqVO updateReqVO) {
        rdrecord10Service.updateRdrecord10(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产入库主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:delete')")
    public CommonResult<Boolean> deleteRdrecord10(@RequestParam("id") Integer id) {
        rdrecord10Service.deleteRdrecord10(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产入库主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:query')")
    public CommonResult<Rdrecord10RespVO> getRdrecord10(@RequestParam("id") String id) {

        try {
            Rdrecord10DO rdrecord10 = rdrecord10Service.getRdrecord10(Integer.valueOf(id));
            return success(BeanUtils.toBean(rdrecord10, Rdrecord10RespVO.class));
        }catch (Exception e)
        {
            Rdrecord10DO  rdrecord10=  rdrecord10Service.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(rdrecord10, Rdrecord10RespVO.class));
        }
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产入库主分页")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:query')")
    public CommonResult<PageResult<Rdrecord10RespVO>> getRdrecord10Page(@Valid Rdrecord10PageReqVO pageReqVO) {
        PageResult<Rdrecord10DO> pageResult = rdrecord10Service.getRdrecord10Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Rdrecord10RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产入库主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRdrecord10Excel(@Valid Rdrecord10PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Rdrecord10DO> list = rdrecord10Service.getRdrecord10Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产入库主.xls", "数据", Rdrecord10RespVO.class,
                        BeanUtils.toBean(list, Rdrecord10RespVO.class));
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:rdrecord01:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        rdrecord10Service.process(id);
        return success(true);
    }

    // ==================== 子表（生产入库子） ====================

    @GetMapping("/rdrecords10/list-by-pid")
    @Operation(summary = "获得生产入库子列表")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:rdrecord10:query')")
    public CommonResult<List<Rdrecords10RespVO>> getRdrecords10ListByPid(@RequestParam("pid") Integer pid) {
        return success(rdrecord10Service.findRdrecords10ByPid(pid));
    }

}